package com.ruoyi.pollution.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import com.ruoyi.base.domain.BasPollutionsource;
import com.ruoyi.base.mapper.BasPollutionsourceMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pollution.mapper.BusPsBasWateroutputMapper;
import com.ruoyi.pollution.domain.BusPsBasWateroutput;
import com.ruoyi.pollution.service.IBusPsBasWateroutputService;

/**
 * 废水排放口Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-19
 */
@Service
public class BusPsBasWateroutputServiceImpl implements IBusPsBasWateroutputService 
{
    @Autowired
    private BusPsBasWateroutputMapper busPsBasWateroutputMapper;
    @Autowired
    private BasPollutionsourceMapper basPollutionsourceMapper;


    /**
     * 查询废水排放口
     * 
     * @param outportcode 废水排放口主键
     * @return 废水排放口
     */
    @Override
    public BusPsBasWateroutput selectBusPsBasWateroutputByOutportcode(String outportcode)
    {
        return busPsBasWateroutputMapper.selectBusPsBasWateroutputByOutportcode(outportcode);
    }

    /**
     * 查询废水排放口列表
     * 
     * @param busPsBasWateroutput 废水排放口
     * @return 废水排放口
     */
    @Override
    public List<BusPsBasWateroutput> selectBusPsBasWateroutputList(BusPsBasWateroutput busPsBasWateroutput)
    {
        return busPsBasWateroutputMapper.selectBusPsBasWateroutputList(busPsBasWateroutput);
    }

    /**
     * 新增废水排放口
     * 
     * @param busPsBasWateroutput 废水排放口
     * @return 结果
     */
    @Override
    public int insertBusPsBasWateroutput(BusPsBasWateroutput busPsBasWateroutput) throws IOException {
        busPsBasWateroutput.setOutportcode(UUID.randomUUID().toString());
        busPsBasWateroutput.setUpdatedate(DateUtils.getNowDate());
        if(StringUtils.isNotEmpty(busPsBasWateroutput.getPolluteid())){
            BasPollutionsource basPollutionsource = basPollutionsourceMapper.selectBasPollutionsourceById(busPsBasWateroutput.getPolluteid());
            if(basPollutionsource == null){
                throw new ServiceException("企业信息不存在");
            }
            busPsBasWateroutput.setPolluteid(basPollutionsource.getId());
            busPsBasWateroutput.setPollutename(basPollutionsource.getPollutename());
        }
        busPsBasWateroutput.setEntcode(UUID.randomUUID().toString());
        if(busPsBasWateroutput.getPhotofile() != null){
            busPsBasWateroutput.setPhoto(busPsBasWateroutput.getPhotofile());
        }
        return busPsBasWateroutputMapper.insertBusPsBasWateroutput(busPsBasWateroutput);
    }

    /**
     * 修改废水排放口
     * 
     * @param busPsBasWateroutput 废水排放口
     * @return 结果
     */
    @Override
    public int updateBusPsBasWateroutput(BusPsBasWateroutput busPsBasWateroutput) throws IOException {
        busPsBasWateroutput.setUpdatedate(DateUtils.getNowDate());
        if(StringUtils.isNotEmpty(busPsBasWateroutput.getPolluteid())){
            BasPollutionsource basPollutionsource = basPollutionsourceMapper.selectBasPollutionsourceById(busPsBasWateroutput.getPolluteid());
            if(basPollutionsource == null){
                throw new ServiceException("企业信息不存在");
            }
            busPsBasWateroutput.setPolluteid(basPollutionsource.getId());
            busPsBasWateroutput.setPollutename(basPollutionsource.getPollutename());
        }
        if(busPsBasWateroutput.getPhotofile() != null){
            busPsBasWateroutput.setPhoto(busPsBasWateroutput.getPhotofile());
        }
        return busPsBasWateroutputMapper.updateBusPsBasWateroutput(busPsBasWateroutput);
    }

    /**
     * 批量删除废水排放口
     * 
     * @param outportcodes 需要删除的废水排放口主键
     * @return 结果
     */
    @Override
    public int deleteBusPsBasWateroutputByOutportcodes(String[] outportcodes)
    {
        return busPsBasWateroutputMapper.deleteBusPsBasWateroutputByOutportcodes(outportcodes);
    }

    /**
     * 删除废水排放口信息
     * 
     * @param outportcode 废水排放口主键
     * @return 结果
     */
    @Override
    public int deleteBusPsBasWateroutputByOutportcode(String outportcode)
    {
        return busPsBasWateroutputMapper.deleteBusPsBasWateroutputByOutportcode(outportcode);
    }
}
